package com.mifengs.mall.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.mifengs.commonbase.util.LogUtil;
import com.mifengs.mall.Constant;
import com.mifengs.mall.R;
import com.mifengs.mall.event.WebFrgEvent;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信回调的activity,注意需要在 自己的包名下建立wxapi 的包 再在下面建立 WXPayEntryActivity
 * 类 注意包名和类名不能错
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);
        api = WXAPIFactory.createWXAPI(this, Constant.WEIXIN_APPID);
        api.handleIntent(getIntent(),this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        LogUtil.i("weixin:",baseReq.toString());
    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if(baseResp.errCode==0){
                WebFrgEvent event = new WebFrgEvent(WebFrgEvent.WEIXIN_PAY);
                event.setBaseResp(baseResp);
                EventBus.getDefault().post(event);
            }
        }
        finish();
    }


}
